<?php
/**
 * $Id: Modules.php 121 2009-02-23 03:58:36Z hzq $
 *
 */
class Datas extends UModel
{
	protected $_name = 'datas';
	
	public function getModules($type,$pid=1)
	{		
		
	}
	
	public function fetchIndex()
	{
		//http://rent.591.com.tw/index.php?firstRow=120&totalRows=46624&module=house&action=rentSale
		//http://sale.591.com.tw/index.php?firstRow=40&totalRows=4703&module=house&action=rentSale
		//http://sale.591.com.tw/sale-detail-40294.html
		//http://rent.591.com.tw/rent-detail-239890.html
	}
	
	public function fetchList($firstRow=0,$totalRows='4702',$datatype='rent', $module='house',$action='rentSale',$pageSize=40)
	{
		if($firstRow>2000)return ;
		if($firstRow==0){
			$data = file_get_contents('http://'.$datatype.'.591.com.tw/');
			$totalRows = $this->saveData($data,true,$datatype);
			$firstRow += $pageSize;
			$this->fetchList($firstRow,$totalRows,$datatype);
		}else {
			
			$url = 'http://'.$datatype.'.591.com.tw/index.php?firstRow='.$firstRow.'&totalRows='.$totalRows.'&module='.$module.'&action='.$action;
			$data = file_get_contents($url);
			$this->saveData($data,false,$datatype);
			$firstRow += $pageSize;
			$this->fetchList($firstRow,$totalRows,$datatype);
		}
	}
	
	public function fetchDetail($datatype='rent')
	{
		//$data = file_get_contents(ROOT_DIR.'/cache/detail.html');
		$where = $this->select()->where('type=?',$datatype);
		$rows = $this->fetchAll($where)->toArray();
		//print_r($rows);exit();
		foreach ($rows as $row){
			$url = 'http://'.$datatype.'.591.com.tw/'.$row['url'];
			$data = file_get_contents($url);
			if($datatype == 'rent'){
				$this->saveDetail($data,$row['id']);
			}elseif($datatype == 'sale'){
				$this->saveSaleDetail($data,$row['id']);
			}
		}
	}
	
	
	
	/**
	 * 保存591出租详细内容。
	 *
	 * @param string $data
	 * @param ing $rid
	 */
	public function saveDetail($data,$rid=1)
	{
		$aData = explode('<div class="detail">',$data);
		if(empty($aData[1]))return ;
		$aData = explode('<div id="MarketInfo"></div>',$aData[1]);
		
		if(empty($aData[0]))return ;
		
		$aImgData = explode('<div id="image" style="display:none" >',$aData[1]);
		if($aImgData[1]){
			$aImgData = explode('<div id="HouseInfo"></div>',$aImgData[1]);
			$sImgData = $aImgData[0];
			preg_match_all('/<a href="([^"]+)".*title="([^"]+)">/i',$sImgData,$matchimgs);
			
			
		}
		
		preg_match_all('/<span class="style5[^"]*">([^<]+)<\/span>/i',$aData[0],$matchs);
		$baseinfo = $matchs[1];
		preg_match_all('/<span class="G style5"><strong>([^<]+)<\/strong><\/span>/i',$aData[0],$matchsize);
		preg_match('/<span class="style5"><span class="Bold">(\d+)<\/span> 房 <span class="Bold">(\d+)<\/span> 廳 <span class="Bold">(\d+)<\/span> 衛<\/span>/i',$aData[0],$matchlayout);
		preg_match('/class="style5 ">([^<]+)<\/td>/i',$aData[0],$matchuse);
		preg_match('/<span class="R style5"><strong>([^<]+)<\/strong><\/span>/i',$aData[0],$matchmoney);
		preg_match('/<span class="style12" style="font-size:9pt">有效期限: ([^<]+)<\/span>/i',$aData[0],$matchvalid);
		preg_match('/<span class="style5"><span class="Bold"> (\d+) <\/span>\/<span class="Bold"> (\d+) <\/span>（出租樓層\/總樓層） <\/span>/i',$aData[0],$matchstype);
		preg_match('/<img src="(include\/tools\/[^"]+)" \/>/i',$aData[0],$matchmobile);
		$nData = explode('<div class="Remark" id="ubbRemark">',$aData[0]);
		$nData = explode('</div></td>',$nData[1]);
		$a = preg_match('/<span class="style5">(.*)<\/span>/i',$nData[1],$matchmark);
		//print_r($matchmobile);
		//preg_match_all('/<span class="style10[^"]*">([^<]+)<\/span>/i',$aData[0],$matchs);
		//print_r($matchs);
		
		//exit();
		
		
		$oneData = array(
			'rid' => $rid ,
			'money' => $matchmoney[1] ,
			'money2' => $baseinfo[1] ,
			'updated' => $baseinfo[0] ,
			'validate' => $matchvalid[1] ,
			'fwgg' => $matchlayout[1].'|'.$matchlayout[2].'|'.$matchlayout[3] ,
			'fwyt' => $matchuse[1] ,
			'fwst' => $baseinfo[2] ,
			'fwps' => $matchsize[1][0] ,
			'fwsx' => $matchsize[1][1] ,
			'llr' => $baseinfo[3] ,
			'email' => $baseinfo[4] ,
			'mobile' => $matchmobile[1] ,
			'tel' => $baseinfo[5] ,
			'glfs' => $baseinfo[6] ,
			'cx' => $baseinfo[7] ,
			'lc' => $matchstype[1].'/'.$matchstype[2] ,
			'dt' => $baseinfo[8] ,
			'wl' => $baseinfo[9] ,
			'cw' => $baseinfo[10] ,
			'kqyr' => $baseinfo[11] ,
			'kf' => $baseinfo[12] ,
			'ycw' => $baseinfo[13] ,
			'xbyq' => $baseinfo[14] ,
			'xfyq' => $baseinfo[15] ,
			'yfdtz' => $baseinfo[16] ,
			'fqjt' => $baseinfo[17] ,
			'dqzl' => $baseinfo[18] ,
			'sfjl' => $baseinfo[19] ,
			'tgsb' => $baseinfo[20] ,
			'tgjj' => $baseinfo[21] ,
			'tssm' => $nData[0] ,
			'fqdb' => $matchmark[1],
		);
		$oDetail = new Details();
		$oDetail->createRow($oneData)->save();
		$oDatapic = new Datapics();
		if(is_array($matchimgs[1]))foreach ($matchimgs[1] as $key=>$img){
			if($key>0)break;
			$imgfile = $img;
			$filebsname = basename($img);
			$oneData = array(
				'rid' => $rid ,
				'pic' => 'upload/product/20090511/'.$filebsname ,
				'title' => $matchimgs[2][$key] 
			);
			$oDatapic->createRow($oneData)->save();
			$saveimgfile = ROOT_DIR.'/public/upload/product/20090511/'.$filebsname;
			if(!file_exists($saveimgfile)){
				$imgct = file_get_contents($imgfile);
				if($imgct)file_put_contents($saveimgfile,$imgct);
			}
			
		}
		
	}
	
	public function saveSaleDetail($data,$rid=1)
	{
		$aData = explode('<div class="detail">',$data);
		if(empty($aData[1]))return ;
		$aData = explode('<div id="MarketInfo"></div>',$aData[1]);
		
		if(empty($aData[0]))return ;
		
		$aImgData = explode('<div id="image"  style="display:none" >',$aData[1]);
		if($aImgData[1]){
			$aImgData = explode('<div id="HouseInfo"></div>',$aImgData[1]);
			$sImgData = $aImgData[0];
			preg_match_all('/<a href="([^"]+)".*title="([^"]+)">/i',$sImgData,$matchimgs);
		}
		
		$aPdata = explode('<span class="style10">特色說明</span>',$aData[0]);
		$aAdata = explode('<td',$aPdata[0]);
		array_shift($aAdata);
		array_pop($aAdata);
		$aNAdata = array();
		foreach ($aAdata as $k=>$val){
			if(($k+1)%2==0)$aNAdata[] = strip_tags('<td '.$val);
		}		
		preg_match_all('/<span class="style10[^"]*">(.*)<\/span>/i',$aPdata[1],$matchs);
		$nData = explode('<div class="Remark" id="ubbRemark">',$aPdata[1]);
		$nData = explode('</div></td>',$nData[1]);
		
		/*print_r($matchs);
		
		exit();*/
		
		$vvdata = explode('有效期限: ',$aNAdata[1]);
		$oneData = array(
			'rid' => $rid ,
			'money' => $aNAdata[0] ,
			'money2' => $aNAdata[2] ,
			'updated' => trim($vvdata[0]) ,
			'validate' => trim($vvdata[1]) ,
			'fwgg' => $aNAdata[8] ,
			'fwyt' => $aNAdata[3] ,
			'fwst' => $aNAdata[4] ,
			'fwps' => $aNAdata[6] ,
			'fwsx' => $aNAdata[11] ,
			'llr' => $aNAdata[12] ,
			'email' => $aNAdata[13] ,
			'mobile' => '' ,
			'tel' => '' ,
			'glfs' => $aNAdata[17] ,
			'cx' => $aNAdata[16] ,
			'lc' => $aNAdata[9] ,
			'dt' => $aNAdata[19] ,
			'wl' => $aNAdata[10] ,
			'cw' => $aNAdata[21] ,
			'kqyr' => $aNAdata[5] ,
			'kf' => $aNAdata[7] ,
			'ycw' => $aNAdata[18] ,
			'xbyq' => $aNAdata[20] ,
			'xfyq' => $aNAdata[22] ,
			'yfdtz' => $aNAdata[23] ,
			'fqjt' => $aNAdata[24] ,
			'dqzl' => '' ,
			'sfjl' => '' ,
			'tgsb' => '' ,
			'tgjj' => '' ,
			'tssm' => $nData[0] ,
			'fqdb' => $matchs[1][1],
		);
		$oDetail = new Details();
		$oDetail->createRow($oneData)->save();
		$oDatapic = new Datapics();
		if(is_array($matchimgs[1]))foreach ($matchimgs[1] as $key=>$img){
			if($key>0)break;
			$imgfile = $img;
			$filebsname = basename($img);
			$oneData = array(
				'rid' => $rid ,
				'pic' => 'upload/product/20090511/'.$filebsname ,
				'title' => $matchimgs[2][$key] 
			);
			$oDatapic->createRow($oneData)->save();
			$saveimgfile = ROOT_DIR.'/public/upload/product/20090511/'.$filebsname;
			if(!file_exists($saveimgfile)){
				$imgct = file_get_contents($imgfile);
				if($imgct)file_put_contents($saveimgfile,$imgct);
			}
		}
		
	}
	
	
	
	/**
	 * 保存591出租/出售列表
	 *
	 * @param string $data
	 * @param bool $isfirst
	 * @return int
	 */
	public function saveData($data,$isfirst = false,$datatype = 'rent')
	{
		//列表：
		if(empty($data))return ;
		$aData = explode('<div id="viewList">',$data);
		if(empty($aData[1]))return ;
		$data = $aData[1];
		$aData = explode('<div class="BaiYe">',$data);
		if(empty($aData[0]))return ;
		$aData = explode('<div class="itemBottom" id="itemBottom">',$aData[0]);
		$a = $aData[0];
		$b = $aData[1];
		$aA = explode('<div class="item isvip" >',$a);
		array_shift($aA);
		$aCitys = Caches::loadCache('city');
		$aArea = Caches::loadCache('area');
		$aCitys = array_flip($aCitys);
		$aArea = array_flip($aArea);
		if(is_array($aA))foreach ($aA as $one){
			preg_match('/<a href="([^"]+)" title="([^"]+)" target="_blank">[^<]+<\/a>/i',$one,$matcharea);
			preg_match('/<span class="areaKind">(\d+)<span>坪<\/span>\/([^<]+)<\/span>/i',$one,$matchsize);
			preg_match('/<span class="price">([^<]+)<\/span>/i',$one,$matchprice);
			preg_match('/<span class="browsenum">(\d+)<\/span>/i',$one,$matchhits);
			$na = explode(' ',$matcharea[2]);
			
			$oneData = array(
				'city' => $aCitys[$na[0]],
				'area' => $aArea[$na[1]],
				'address' => $na[2],
				'size' => $matchsize[1],
				'useage' => $matchsize[2],
				'price' => $matchprice[1],
				'hits' => $matchhits[1],
				'addtime' => time(),
				'url' => $matcharea[1],
				'type' => $datatype,
			);
			
			$this->createRow($oneData)->save();
		}
		if($isfirst){
			preg_match('/共 <span class="R">(\d+) <\/span>筆/i',$b,$matchs);
			
			return $matchs[1]?$matchs[1]:1000;
		}
	}
}